<?php

class Dblib
{
	

/*
input: query command, type of fetch command

output array for smarty
*/
	

	public function query($strquery,$type)
	{
		
		
		$result = mysql_query($strquery);
		
		$data = array();
		
		if (mysql_num_rows($result) >= 1)
		{
			while ($line = mysql_fetch_array($result, $type)) {
				$data[] = $line;
			}
			mysql_free_result($result);
		}
		return $data;
	}
	
	//
	public	function anti_sql($data) {
	
		$sql = preg_replace(sql_regcase("/(#|\*|--|\\\\)/"),"",$sql);
		return trim(strip_tags(addslashes($sql))); #strtolower()
		
		/*global $link;
		if (ini_get('magic_quotes_gpc')) {
			$data = stripslashes($data);
		}
		return mysql_real_escape_string($data, $link);*/
	}
	

	
	
	public function query_cell($strquery,$type,$column)
	{
		$result = mysql_query($strquery);
		$data = '';
		if (mysql_num_rows($result) >= 1){
			while ($line = mysql_fetch_array($result, $type)) 
			{
				$data = $line[$column];
			}
			mysql_free_result($result);
		}
		return $data;
	}
	
	public function query_exist($strquery)
	{
		$result = mysql_query($strquery);
		
		
		if (mysql_num_rows($result) >= 1) 
		{
			return true;
		}
		else 
		{
			return false;
		}
		
				
	}
	
	public function query_object($strquery)
	{
		$result = mysql_query($strquery);
	
		$object = mysql_fetch_object($result); 
		
		mysql_free_result($result);
		return $object;
	}
	
	public function getmaxRanking($table,$column)
    {
    	
		$sql = "SELECT MAX(`$column`) as `max` FROM `$table`";
	
		$max_ranking = $this->query_cell($sql,MYSQL_ASSOC,'max');
		
		return $max_ranking + 1;
    }
    
    
	public function escape_data ($data) 
	{
		global $link;
		if (ini_get('magic_quotes_gpc')) {
			$data = stripslashes($data);
		}
		return mysql_real_escape_string($data, $link);
	}

	 public function code_exist($table,$column,$code)
    {
    		
					
  		$where = "`$column` = '".$code."'" ;
  		
  		$sql = "SELECT * FROM `$table` where $where";
  	
		if ($this->query_exist("$sql")) {
			return true;
		}
	
		else 
		{
					return false;
		}
				
		
    }
	
}

?>